package com.lxy.file.enums;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/** 通用枚举
 * @author LiuXiaoYu
 * @date 2021/7/19- 9:42
 */
@Getter
public enum ExceptionEnum {
    /**
     * 通用枚举结果
     */
    UPOLAOD_FILE_FAIL(40001,"文件上传失败"),
    SUCCESS(200,"成功"),
    ERROR(500,"失败"),
    REQUEST_PATH(404,"请求路径未找到"),
    ALREADY_EXIST(100, "文件已存在"),
    NO_UPLOAD(101, "该文件没有上传过"),
    PART_UPLOAD(102, "该文件上传了一部分"),
    NO_EXIST(103,"部分上传成功或从未上传过"),
    CONNECT_EXCEPTION(40002,"连接异常");

    private final Integer code;
    private final String desc;


    ExceptionEnum(Integer code, String desc){
        this.code=code;
        this.desc=desc;
    }

    private static final Map<Integer, ExceptionEnum> CACHE =new HashMap<>();

    static{
        for (ExceptionEnum exceptionEnum: ExceptionEnum.values()){
            CACHE .put(exceptionEnum.code,exceptionEnum);
        }
    }

     public static String getDesc(Integer code){
        return Optional.ofNullable(CACHE .get(code))
                .map(ExceptionEnum::getDesc)
                .orElseThrow(()-> new IllegalArgumentException("invalid exception code!"));
     }
}
